<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="计划编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanCode">
              <a-input v-model="model.coursePlanCode" placeholder="请输入计划编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计划名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanName">
              <a-input v-model="model.coursePlanName" placeholder="请输入计划名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学年名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="schoolYearId">
              <a-select v-model="model.schoolYearId" placeholder="请选择学年"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in schoolYearDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学期名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="semesterId">
              <a-select v-model="model.semesterId" placeholder="请选择学期"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in semesterDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="年级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeId">
              <a-select v-model="model.gradeId" placeholder="请选择年级"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in gradeDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="科目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">-->
<!--              <a-select v-model="model.subjectId" placeholder="请选择任教科目"-->
<!--                        :getPopupContainer="(target) => target.parentNode">-->
<!--                <a-select-option v-for="item in subjectList" :key="item.value">{{ item.label }}</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="计划生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanStartDate">
              <j-date placeholder="请选择计划生效日期" v-model="model.coursePlanStartDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计划失效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanEndDate">
              <j-date placeholder="请选择计划失效日期" v-model="model.coursePlanEndDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计划状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanStatus">
              <a-select  v-model="model.coursePlanStatus"  placeholder="请选择计划状态"  :getPopupContainer= "(target) => target.parentNode">
                <a-select-option :value="1">编制</a-select-option>
                <a-select-option :value="2">发布</a-select-option>
                <a-select-option :value="3">应用</a-select-option>
                <a-select-option :value="4">过期</a-select-option>
                <a-select-option :value="5">下线</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
              <a-select  v-model="model.izOpen"  placeholder="请选择计划状态"  :getPopupContainer= "(target) => target.parentNode">
                <a-select-option :value="1">是（启用）</a-select-option>
                <a-select-option :value="0">否（未启用）</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="是否删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izDelete">-->
<!--              <a-select  v-model="model.izDelete"  placeholder="请选择删除状态"  :getPopupContainer= "(target) => target.parentNode">-->
<!--                <a-select-option :value="1">是（删除）</a-select-option>-->
<!--                <a-select-option :value="0">否（不删除）</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">-->
<!--              <j-date placeholder="请选择创建时间" v-model="model.createTime"  style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="编辑时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="editTime">-->
<!--              <j-date placeholder="请选择编辑时间" v-model="model.editTime"  style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { getSchoolYearDataList,getSemesterDataList,getAllGrade,getSubjectList } from '@api/commonAPI'

  export default {
    name: 'CouCoursePlanForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/courseplan/couCoursePlan/add",
          edit: "/courseplan/couCoursePlan/edit",
          queryById: "/courseplan/couCoursePlan/queryById"
        },
        semesterDataList: [],
        schoolYearDataList: [],
        gradeDataList: [],
        subjectList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getSchoolYearData();
      this.getSemesterData();
      this.getGradeData();
      this.getSubjectData();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      async getSchoolYearData() {
        let res = await getSchoolYearDataList()
        this.schoolYearDataList = []
        res.result.forEach(item => {
          this.schoolYearDataList.push({
            label: item.schoolYearName,
            value: item.id
          })
        })
      },
      async getSemesterData() {
        let res = await getSemesterDataList()
        this.semesterDataList = []
        res.result.forEach(item => {
          this.semesterDataList.push({
            label: item.semesterName,
            value: item.id
          })
        })
      },
      async getGradeData() {
        let res = await getAllGrade()
        this.gradeDataList = []
        res.result.forEach(item => {
          this.gradeDataList.push({
            label: item.gradeName,
            value: item.id
          })
        })
      },
      async getSubjectData() {
        let res = await getSubjectList()
        this.subjectList = []
        res.result.forEach(item => {
          this.subjectList.push({
            label: item.subjectName,
            value: item.id
          })
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>